package training;

import java.util.ArrayDeque;
import java.util.Deque;
import java.util.stream.IntStream;

/**
 * @Author liangzai
 * @Description:
 */
public class J25_7_30_Training {
    //按位与最大的最长子数组
    public int longestSubarray(int[] nums) {
        // 就是找最大数最长相同的子数组
        int max = IntStream.of(nums).max().getAsInt(), res = 1;
        for (int i = 0, l = -1, r = -1; i < nums.length; i++) {
            if (nums[i] == max) {
                r = i;
                res = Math.max(res, r - l);
            } else {
                l = i;
            }
        }
        return res;
    }


    //轮转数组
    public void rotate(int[] nums, int k) {
        Deque<Integer> deque = new ArrayDeque<>();

        //装填队列
        for (int num : nums){
            deque.offer(num);
        }

        while(k-- > 0){
            deque.offerFirst(deque.pollLast());
        }

        for (int i = 0; i < nums.length; i++){
            nums[i] = deque.poll(); //覆盖原数组
        }
    }
}
